//
//  HLHomeCellTopView.swift
//  weibo-snh
//
//  Created by 黄力 on 15/12/20.
//  Copyright © 2015年 HLTest. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class HLHomeCellTopView: UIView {
    
    //保存当前底部约束
    var topViewBottomConstraint: Constraint?
    
    //接收状态数据的属性
    var homeStatus: HLHomeStatuses?{
        didSet {
            //给子控件设置内容
            //头像
            iconView.sd_setImageWithURL(homeStatus?.user?.avatar_URL ?? nil)
            //认证
            verifiedView.image = homeStatus?.user?.verified_Image
            //昵称
            nameLabel.text = homeStatus?.user?.name
            //会员图像
            vipView.image = homeStatus?.user?.vipImage
            //TODO: 设置时间和来源
            //时间
//            timeLabel.text = homeStatus?.created_at
            //来源
//            sourceLabel.text = homeStatus?.source
            //内容
            contentLabel.text = homeStatus?.text
            //根据图片个数来设置图片控件约束
            picturesView.picturesUrlArr = homeStatus?.imageURLArr
            //取消上次的约束
            topViewBottomConstraint?.uninstall()
            //根据有没有图片来修改约束
            guard let imageArr = homeStatus?.imageURLArr where imageArr.count > 0 else{
                //没有图片
                self.snp_updateConstraints(closure: { (make) -> Void in
                    topViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(homeCellTopViewMargin).constraint
                })
                return
            }
            //有图片
            self.snp_updateConstraints(closure: { (make) -> Void in
                topViewBottomConstraint = make.bottom.equalTo(picturesView).offset(homeCellTopViewMargin).constraint
            })
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        //设置界面
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置界面
    private func setupUI() {
        //添加子控件
        addSubview(iconView)
        addSubview(verifiedView)
        addSubview(nameLabel)
        addSubview(vipView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(picturesView)
        
        //设置约束
        //头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.left.equalTo(self).offset(homeCellTopViewMargin)
            make.size.equalTo(CGSizeMake(homeCellTopViewIconWidth, homeCellTopViewIconWidth))
        }
        //认证图片
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right).offset(-homeCellTopViewIconWidth * 0.1)
            make.centerY.equalTo(iconView.snp_bottom).offset(-homeCellTopViewIconWidth * 0.1)
        }
        //昵称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(homeCellTopViewMargin)
        }
        //vip图像
        vipView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.left.equalTo(nameLabel.snp_right).offset(homeCellTopViewMargin)
        }
        //时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(homeCellTopViewMargin)
        }
        //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.left.equalTo(timeLabel.snp_right).offset(homeCellTopViewMargin)
        }
        //文本
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(homeCellTopViewMargin)
            make.left.equalTo(iconView)
            make.right.equalTo(self).offset(-homeCellTopViewMargin)
        }
        //图片
        picturesView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(homeCellTopViewMargin)
        }
        
        //设置自身约束
        self.snp_makeConstraints { (make) -> Void in
            topViewBottomConstraint = make.bottom.equalTo(picturesView.snp_bottom).offset(homeCellTopViewMargin).constraint
        }
        
    }
    
    // MARK: 懒加载子控件
    //头像视图
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView()
        //设置头像占位图
        imageView.image = UIImage(named: "avatar_default_big")
        //设置圆角并裁减
        imageView.layer.cornerRadius = homeCellTopViewIconWidth * 0.5
        imageView.layer.masksToBounds = true
        return imageView
    }()
    //昵称
    private lazy var nameLabel: UILabel = UILabel(title: "吴彦祖", titleColor: UIColor.orangeColor(), titleFont: 12)
    //认证图像
    private lazy var verifiedView: UIImageView = UIImageView(image: UIImage(named: "avatar_enterprise_vip"))
    //会员图像
    private lazy var vipView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    //发送时间
    private lazy var timeLabel: UILabel = UILabel(title: "11:11", titleColor: UIColor.orangeColor(), titleFont: 10)
    //来源
    private lazy var sourceLabel: UILabel = UILabel(title: "来自星星", titleColor: UIColor.orangeColor(), titleFont: 10)
    //文本
    private lazy var contentLabel: UILabel = UILabel(title: "我划船从来不用桨,全靠浪,浪浪浪浪浪浪浪浪~", titleColor: UIColor.lightGrayColor(), titleFont: 14, center: false)
    //图片
    private lazy var picturesView: HLHomeCellPicturesView = HLHomeCellPicturesView()
}
